﻿Imports System
Imports System.Windows.Forms
Imports System.IO
Imports TBCC.UtilityMobile
Public Class ProjectListForm


    Private WithEvents _projectList As New Hashtable()
    Public Update_Flag As Boolean = False

    '终端被选择事件
    '  Public Event ProjectSelected(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs)


    Public ReadOnly Property ProjectList() As Hashtable
        Get
            Return _projectList
        End Get
    End Property


    Private Sub ProjectListForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        FillDataGridViewColumn()

        LoadProjects()

    End Sub


    Public Sub LoadProjects()

        Me.dgvProjectList.Rows.Clear()
        _projectList.Clear()

        Dim il As IList(Of ProjectInfo.CarProjectInfo)
        il = CarProjectService.GetAllCarProjectInfo()

        'For i As Integer = 0 To 9
        '    Dim pi As New ProjectItem()
        '    pi.ProjectName = Now.AddDays(i)
        '    pi.ProjectMacID = Now.AddDays(i)
        '    il.Add(pi)
        'Next

        If il IsNot Nothing Then
            Dim pi As ProjectInfo.CarProjectInfo
            For i As Integer = 0 To il.Count - 1
                pi = il(i)
                Dim row As DataGridViewRow = GetNewRow()
                row.Cells(0).Value = i + 1
                If pi.CarProjectType > 0 Then
                    row.Cells(1).Value = ProjectTypeList(pi.CarProjectType)
                Else
                    row.Cells(1).Value = Nothing
                End If
                row.Cells(2).Value = pi.CarProjectName
                'If pi.LocalRunStatus > 0 Then
                '    row.Cells(3).Value = RunStatusList(pi.LocalRunStatus - 1)
                'Else
                row.Cells(3).Value = pi.ProCode
                'End If
                ' row.Cells(9).Value = pi
                row.Tag = pi
                Me.dgvProjectList.Rows.Add(row)
                '_projectList.Add(pi.ProjectMacID, row)
            Next
        End If

    End Sub


#Region " DataGridView操作 "

    '创建新行
    Private Function GetNewRow() As DataGridViewRow
        Dim row As New DataGridViewRow()
        Dim cell As DataGridViewCell

        cell = New DataGridViewTextBoxCell()
        row.Cells.Add(cell)

        cell = New DataGridViewTextBoxCell()
        row.Cells.Add(cell)

        cell = New DataGridViewTextBoxCell()
        row.Cells.Add(cell)

        cell = New DataGridViewTextBoxCell()
        row.Cells.Add(cell)

        cell = New DataGridViewTextBoxCell()
        row.Cells.Add(cell)

        cell = New DataGridViewTextBoxCell()
        row.Cells.Add(cell)

        cell = New DataGridViewTextBoxCell()
        row.Cells.Add(cell)

        cell = New DataGridViewTextBoxCell()
        row.Cells.Add(cell)

        cell = New DataGridViewTextBoxCell()
        row.Cells.Add(cell)

        cell = New DataGridViewTextBoxCell()
        row.Cells.Add(cell)

        Return row
    End Function

    '
    Private Sub dgvProjectList_RowEnter(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvProjectList.RowEnter

        '  RaiseEvent ProjectSelected(sender, e)

    End Sub

#End Region

    '[修改终端]
    Private Sub tsmiEditPrj_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsmiEditPrj.Click, dgvProjectList.DoubleClick
        'Dim pi As ProjectItem
        'pi = GetSelectedItem()
        'If pi Is Nothing Then
        '    'msg.Show("请选择要修改的终端。")
        '    Exit Sub
        'End If

        'Dim frm As New ProjectBasicInfoForm(pi)
        'If frm.ShowDialog = Windows.Forms.DialogResult.OK Then
        '    Update_Flag = True '参数发生修改
        '    LoadProjects()
        'End If
    End Sub

    '[删除终端]
    Private Sub tsmiDelPrj_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsmiDelPrj.Click
        'Dim pi As ProjectItem
        'pi = GetSelectedItem()
        'If pi Is Nothing Then
        '    'msg.Show("请选择要删除的终端。")
        '    Exit Sub
        'End If

        'Dim tip As String
        'tip = String.Format("是否要删除终端【{0}】的所有配置信息和历史数据？", pi.ProjectName)
        'If msg.ShowYesNo(tip) = Windows.Forms.DialogResult.Yes Then
        '    If ProjectService.Delete(pi, ProjectService.DevTypeMaxNetid(pi.ProjectID)) = True Then
        '        Update_Flag = True '参数发生修改
        '        Me.LoadProjects()
        '        msg.Show("删除成功。")
        '    Else
        '        msg.ShowError("删除失败。")
        '    End If
        'End If
    End Sub

    '[启停纪录]
    Private Sub tsmiStartup_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsmiStartup.Click
        'Dim pi As ProjectItem
        'pi = GetSelectedItem()
        'If pi Is Nothing Then
        '    Exit Sub
        'End If

        'Dim frm As New StartupViewForm(pi, False)
        'frm.Show(Me)
    End Sub

    '[历史数据]
    Private Sub tsmiPrjHisData_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsmiPrjHisData.Click
        'Dim pi As ProjectItem
        'pi = GetSelectedItem()
        'If pi Is Nothing Then
        '    Exit Sub
        'End If

        'Dim frm As New HDViewForm(pi)
        'frm.Show(Me)
    End Sub

    Private Function GetSelectedItem() As ProjectInfo.CarProjectInfo
        If Me.dgvProjectList.CurrentRow Is Nothing Then
            Return Nothing
        End If

        Dim pi As ProjectInfo.CarProjectInfo
        pi = CType(Me.dgvProjectList.CurrentRow.Tag, ProjectInfo.CarProjectInfo)
        Return pi

    End Function

    Private Sub FillDataGridViewColumn()
        Me.dgvProjectList.Columns(9).AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
        Me.dgvProjectList.Columns(9).AutoSizeMode = DataGridViewAutoSizeColumnMode.NotSet
    End Sub

    Private Sub ProjectListForm_Resize(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Resize
        FillDataGridViewColumn()
    End Sub

    'Private Sub mmnuMailCfg_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mmnuMailCfg.Click
    '    Dim pi As ProjectInfo.CarProjectInfo
    '    pi = GetSelectedItem()
    '    If pi Is Nothing Then
    '        MessageBox.Show("请选择要配置的工程")
    '        Exit Sub
    '    End If

    '    Dim mailcfg As New CarMailCfg(pi.ProId, pi.CarProjectName)
    '    mailcfg.ShowDialog(Me)
    'End Sub

    Private Sub UploadData_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles UploadData.Click
        Dim pi As ProjectInfo.CarProjectInfo
        pi = GetSelectedItem()
        If pi Is Nothing Then
            MessageBox.Show("请选择要上传的小批零")
            Exit Sub
        End If
        Me.pnlSearch.Visible = True
        Me.bwSearch.RunWorkerAsync(New Object() {pi})
        'PacketUpload.instance.PacketAutoUpload(pi.ProId, pi.ProCode)

    End Sub
    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="curProjectId">小批零记录仪设备的工程PACID</param>
    ''' <param name="curDevId">记录仪的设备编号</param>
    ''' <returns>false:上传失败; true:上传成功</returns>
    Public Function PacketAutoUpload(ByVal curProjectId As String, ByVal curDevId As String)

        Dim lineStr As String
        Dim isFindDisk As Boolean = False
        Dim count As Integer = 0
        Try
            Dim fso, drv
            Dim diskDriver = "C:"
            fso = CreateObject("scripting.FileSystemObject")

            '检查每一个驱动器是否为记录仪盘
            For Each drv In fso.Drives
                Try
                    If drv.VolumeName.indexOf("WASON") = 0 And drv.TotalSize < 10000000 And drv.IsReady = True Then '检查是否存在wason命名的卷标，并且磁盘空间小于10M,而且只读
                        diskDriver = drv.Path  '记录驱动器名
                        isFindDisk = True       '标记找到驱动器

                        If MessageBox.Show("检测到小批零设备,是否上传数据?", "选择", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = DialogResult.No Then
                            Return False
                        End If
                        Exit For
                    End If
                Catch ex As Exception
                    '跳过不能获取的驱动器(例如没有插入盘的光盘驱动器和软盘驱动器)
                    Continue For
                End Try
            Next

            If isFindDisk = False Then  '没有找到小批零设备
                MessageBox.Show("无法检测到小批零设备，请确保设备与电脑正确连接", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information)
                Return False
            End If

            ' 检查是否设备中至少存在一个文件
            Dim colfile
            Dim objFolder = fso.GetFolder(diskDriver)
            Dim colFiles = objFolder.Files
            Dim objFileName = "C:"
            Dim isFindFile As Boolean = False
            For Each colfile In objFolder.Files
                isFindFile = True        '标记找到至少一个文件
                objFileName = colfile.Path
                Exit For
            Next

            If isFindFile = False Then   '设备中没有一个记录文件
                MessageBox.Show("小批零设备中不存在数据记录", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information)
                Return False
            End If

            Dim str As String = String.Format("正在上传{0}小批零数据……", curDevId)
            If Me.InvokeRequired Then
                Me.Invoke(New VoidMethodHandler(AddressOf SetSearchState), str)
            Else
                SetSearchState(str)
            End If

            ' 读取第一个文件中的设备编号，文件中不存在设备编号或设备号与本地小批零记录仪列表中的设备编号不同
            Dim objStream = fso.OpenTextFile(objFileName)
            Dim devId
            Dim errDevice As Boolean = True
            lineStr = objStream.ReadLine()
            While lineStr IsNot Nothing
                If lineStr.IndexOf("Serial No.,") >= 0 Then  '包括"Serial No"标识
                    devId = lineStr.Substring(lineStr.IndexOf("Serial No.,") + 11)
                    If devId.indexOf(curDevId) <> -1 Then '设备编号与工程配置一致
                        errDevice = False
                        Exit While
                    Else '实际接入电脑的设备编号与网站列表中的选择上传的设备编号不同
                        MessageBox.Show("待上传设备与已配置的编号不一致，请重新选择", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information)
                        Exit While
                    End If
                End If
                lineStr = objStream.ReadLine()
            End While

            objStream.Close()

            If errDevice = True Then
                Return False
            End If
            Dim result As Boolean = False

          
            '检查并获取当前设备所有启停记录时间数据
            Dim recordInterval, updateStatus, uplimit, dwlimit
            Dim StartTime, StopTime As DateTime
            For Each colfile In objFolder.Files '遍历记录仪中的所有文件
                objFileName = fso.GetBaseName(colfile.Path) '当前文件名

                '设置启动时间`
                StartTime = objFileName.ToString().Replace(".", ":").Replace(".", ":")  '启动时间
                StopTime = objFileName.ToString().Replace(".", ":").Replace(".", ":")  '停止时间默认设置与启动时间一致
                recordInterval = 60              '记录间隔
                updateStatus = 2                 '记录状态
                uplimit = 8                     '报警上限
                dwlimit = 2                     '报警下限

                '查询该设备中当前启停记录是否在本地数据库中已经存在
                '需要根据启动时间(不需要查询结束时间)查询数据库是否存在该启停记录

                Dim CarStartStop As List(Of ProjectInfo.CarHDStartUpItem)
                CarStartStop = CarHDStartUpService.GetHDStartUpBoxList(curProjectId)


                If CarStartStop Is Nothing Then
                Else
                    For Each CarStartStopItem As ProjectInfo.CarHDStartUpItem In CarStartStop
                        Dim Stime As String = UtilityClass.GetDateString(StartTime)

                        If UtilityClass.GetDateString(CarStartStopItem.BeginTime) = Stime Then
                            result = True
                            Exit For


                        End If

                    Next

                End If



                If result Then '本地数据库已经存在，无需上传，直接跳过
                    result = False
                    Continue For
                End If

                Dim ParentId As Integer = CarHDStartUpService.GetMaxHDStartUpCar(curProjectId) + 1
                Dim index As Integer = 0
                Dim strSql As String
                Dim tic As New TBCC.UtilityMobile.TranstrationItem
                Dim strSql1 As String
                Dim tic1 As New TBCC.UtilityMobile.TranstrationItem



                Dim isAlarm As Boolean = False
                Dim AlarmStatus
                Dim aiValue, minValue, maxValue As Double
                Dim splitStr1 As String()
                Dim splitStr2 As String()
                Dim firstDataTime As DateTime
                Dim secondDataTime As DateTime
                Dim HistDataList As IList(Of String) = New List(Of String)
                Dim isStop As Boolean = False
                '详细解析文件中的每一行，获取详细历史记录信息
                objStream = fso.OpenTextFile(colfile.Path, 1)
                Try
                    lineStr = objStream.ReadLine()
                Catch ex As Exception '没有停止的情况下，可能会读取到文件的eof
                    Continue For  '当前文件为空，继续下一个文件
                End Try

                While lineStr IsNot Nothing  '检查本地文件
                    If (lineStr.Length <= 2) Then '跳过空行

                        Try
                            lineStr = objStream.ReadLine()
                        Catch ex As Exception '没有停止的情况下，可能会读取到文件的eof
                            isStop = False '该记录尚未停止
                            Exit While
                        End Try

                        Continue While
                    End If


                    If lineStr.IndexOf("Date,Time,T.(C),Note") >= 0 Then  '包括"Date,Time,T.(C),Note"标识
                        Try
                            lineStr = objStream.ReadLine()
                        Catch ex As Exception '没有停止的情况下，可能会读取到文件的eof
                            isStop = False '该记录尚未停止
                            Exit While
                        End Try
                        Continue While
                    End If

                    If lineStr.IndexOf("Model,WS-T10PRO") >= 0 Then  '包括"Model,WS-T10PRO"标识
                        Try
                            lineStr = objStream.ReadLine()
                        Catch ex As Exception '没有停止的情况下，可能会读取到文件的eof
                            isStop = False '该记录尚未停止
                            Exit While
                        End Try
                        isStop = True '该记录已经停止
                        Continue While
                    End If

                    If lineStr.IndexOf("Serial No.,") >= 0 Then   '包括"Serial No.,"标识
                        Try
                            lineStr = objStream.ReadLine()
                        Catch ex As Exception '没有停止的情况下，可能会读取到文件的eof
                            isStop = False '该记录尚未停止
                            Exit While
                        End Try
                        isStop = True '该记录已经停止
                        Continue While
                    End If

                    If lineStr.IndexOf("Alarm Max.,,") = 0 Then '报警上限
                        uplimit = Double.Parse(lineStr.Substring(lineStr.IndexOf("Alarm Max.,,") + 12))
                        Try
                            lineStr = objStream.ReadLine()
                        Catch ex As Exception '没有停止的情况下，可能会读取到文件的eof
                            isStop = False '该记录尚未停止
                            Exit While
                        End Try
                        isStop = True '该记录已经停止
                        Continue While
                    End If

                    If lineStr.IndexOf("Alarm Min.,,") = 0 Then '报警下限
                        dwlimit = Double.Parse(lineStr.Substring(lineStr.IndexOf("Alarm Min.,,") + 12))
                        Try
                            lineStr = objStream.ReadLine()
                        Catch ex As Exception '没有停止的情况下，可能会读取到文件的eof
                            isStop = False '该记录尚未停止
                            Exit While
                        End Try
                        isStop = True '该记录已经停止
                        Continue While
                    End If

                    If lineStr.IndexOf("Max.,,") = 0 Then   '最大值
                        maxValue = Double.Parse(lineStr.Substring(lineStr.IndexOf("Max.,,") + 6))
                        If (maxValue > uplimit) Then '超高报警
                            isAlarm = True
                        End If
                        isStop = True '该记录已经停止
                    End If

                    If lineStr.IndexOf("Min.,,") = 0 Then '最小值
                        minValue = Double.Parse(lineStr.Substring(lineStr.IndexOf("Min.,,") + 6))
                        If minValue < dwlimit Then '超低报警
                            isAlarm = True
                        End If
                        isStop = True '该记录已经停止
                        Exit While   '跳过读取文件最后一行导致抛出异常的情况
                    End If

                    If lineStr.Length >= 18 Then '更新有效的历史数据到本地暂存
                        If lineStr.IndexOf(",A") <> -1 Then '该条历史记录中包括报警标识
                            lineStr = lineStr.Substring(0, lineStr.Length - 2) '过滤掉显示报警的标识
                        End If
                        HistDataList.Add(lineStr)

                        Try
                            lineStr = objStream.ReadLine()
                        Catch ex As Exception '没有停止的情况下，可能会读取到文件的eof
                            isStop = False '该记录尚未停止
                            Exit While
                        End Try
                        Continue While
                    End If

                    Try
                        lineStr = objStream.ReadLine()
                    Catch ex As Exception '没有停止的情况下，可能会读取到文件的eof
                        isStop = False '该记录尚未停止
                        Exit While
                    End Try


                End While

                objStream.Close()  '关闭当前操作文件

                If isStop = False Then  '记录只启动尚未停止
                    Continue For  '扫描下一个文件，跳过当前文件
                End If

                Dim tempStr As String
                Dim UpdateTime As String
                Dim sqlrul As Boolean = False
                If HistDataList IsNot Nothing Then
                    'Dim r(17) As MySql.Data.MySqlClient.MySqlParameter
                    'For k As Integer = 0 To r.GetUpperBound(0)
                    '    r(k) = New MySql.Data.MySqlClient.MySqlParameter
                    '    r(k).ParameterName = "r" + k.ToString
                    'Next

                    For i As Integer = 0 To HistDataList.Count - 1  '处理暂存的历史数据记录
                        lineStr = HistDataList.Item(i)

                        '记录最后一条记录时间作为停止时间				  
                        tempStr = lineStr.Substring(0, lineStr.IndexOf(",")) '获取年月日
                        splitStr1 = tempStr.Split("-")
                        If splitStr1(1).Length = 1 Then '月份处理
                            splitStr1(1) = "0" + splitStr1(1)
                        End If
                        If splitStr1(2).Length = 1 Then
                            splitStr1(2) = "0" + splitStr1(2)
                        End If
                        UpdateTime = splitStr1(0) + "-" + splitStr1(1) + "-" + splitStr1(2)

                        tempStr = lineStr.Substring(lineStr.IndexOf(",") + 1, lineStr.LastIndexOf(",") - (lineStr.IndexOf(",") + 1)) '获取时分秒
                        splitStr2 = tempStr.Split(":")
                        If splitStr2(0).Length = 1 Then '小时处理
                            splitStr2(0) = "0" + splitStr2(0)
                        End If
                        If splitStr2(1).Length = 1 Then '分钟处理
                            splitStr2(1) = "0" + splitStr2(1)
                        End If
                        If splitStr2(2).Length = 1 Then '秒钟处理
                            splitStr2(2) = "0" + splitStr2(2)
                        End If

                        UpdateTime = UpdateTime + " " + splitStr2(0) + ":" + splitStr2(1) + ":" + splitStr2(2)

                        If i = 0 Then '记录第一条记录的时间
                            firstDataTime = DateTime.Parse(UpdateTime)
                        End If

                        If i = 1 Then  '计算第二条记录与第一条之间的时间间隔
                            secondDataTime = DateTime.Parse(UpdateTime)
                            recordInterval = (secondDataTime.Subtract(firstDataTime)).TotalSeconds  '获取以秒为单位的时间间隔			   						  
                        End If

                        '采用最后一条记录作为停止时间
                        StopTime = UpdateTime

                        '计算是否本条历史记录超高限或超低限报警

                        '获取AI值
                        aiValue = Double.Parse(lineStr.Substring(lineStr.LastIndexOf(",") + 1))

                        If aiValue >= uplimit Or aiValue <= dwlimit Then   '超高限或超低限报警
                            AlarmStatus = 1

                        Else  '无报警
                            AlarmStatus = 2
                        End If

                        '更新详细历史记录至数据库
                        'aiValue表示当前AI探头值，AlarmStatus表示当前记录报警状态,UpdateTime表示当前记录的时间
                        'todo..

                        strSql1 = String.Format("Insert Into TbccHistData_Box_{18}_1(ParentId,UpdateTime,AI1,AI2,AI3,AI4,Latitude_dir,Latitude,Longitude_dir,Longitude,AlarmStatus,fdapfinished,TransportState,histAlarmStorageType,gpsStorageType,histDataStorageType,unloadStorageType,histAlarmData)" _
                                                & " Values('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}','{9}','{10}','{11}','{12}','{13}','{14}','{15}','{16}','{17}') ", _
                                     ParentId, UpdateTime, aiValue, -300, -300, -300, 0, 0, 0, 0, AlarmStatus, 0, 0, 0, 0, 0, 0, 0, curProjectId)


                       

                        tic1.Add(strSql1, Nothing)






                    Next
                    sqlrul = SqlHelper.ExecuteTranstration(tic1)

                    HistDataList.Clear()  '清除缓存中的详细历史数据记录

                End If

                '更新启停记录至数据库
                'StartTime表示启动时间,StopTime表示停止时间,recordInterval表示记录间隔,updateStatus 表示记录上传状态,uplimit表示报警上限,dwlimit表示报警下限
                index = 0
                strSql = String.Format("Insert Into tbcchiststartup_{0}_1(id,BeginTime,EndTime,RecordInterval,BeginAddress,EndAddress,Shipper,Carrier,Receiver,UpdateStatus,Finished,tlimitType,sent,tuplimit,tdwlimit,Fdapfinished) Values(?p0,?p1,?p2,?p3,?p4,?p5,?p6,?p7,?p8,?p9,?p10,?p11,0,?p12,?p13,?p14);-- ", curProjectId)
                Dim p(14) As MySql.Data.MySqlClient.MySqlParameter
                For i As Integer = 0 To p.GetUpperBound(0)
                    p(i) = New MySql.Data.MySqlClient.MySqlParameter
                    p(i).ParameterName = "p" + i.ToString
                Next
                p(0).Value = ParentId
                p(1).Value = StartTime
                p(2).Value = StopTime
                p(3).Value = recordInterval
                p(4).Value = ""
                p(5).Value = ""
                p(6).Value = ""
                p(7).Value = ""
                p(8).Value = ""
                p(9).Value = updateStatus
                p(10).Value = 1
                p(11).Value = 0
                p(12).Value = uplimit
                p(13).Value = dwlimit
                p(14).Value = 0

                tic.Add(strSql & index.ToString, p)

                If sqlrul Then

                    SqlHelper.ExecuteTranstration(tic)

                End If



                sqlrul = False

                count += 1  '增加提交的启停记录数量
            Next

        Catch ex As Exception
            MessageBox.Show("小批零设备读取失败", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information)
            Return False
        End Try

        If count > 0 Then
            MessageBox.Show("小批零记录上传成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information)
        Else
            MessageBox.Show("不存在需要上传的小批零记录", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information)
        End If


        Return True
    End Function

    Private Sub mmnuMailCfg_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mmnuMailCfg.Click

    End Sub
    Private Delegate Sub VoidMethodHandler(ByVal obj As Object)
    Private Sub SetSearchState(ByVal obj As Object)
        Me.lblSearchState.Text = obj
    End Sub
    Private Sub bwSearch_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles bwSearch.DoWork
        Dim pi As ProjectInfo.CarProjectInfo = e.Argument(0)
        Dim ret As Object
        e.Result = New Object() {True, Nothing}

      

        ret = PacketAutoUpload(pi.ProId, pi.ProCode)

        'If Me.InvokeRequired Then
        '    e.Result(0) = Me.Invoke(New BoolMethodHandler(AddressOf AddDataToGridView), ret)
        'Else
        '    e.Result(0) = AddDataToGridView(ret)
        'End If

        e.Result(1) = bwSearch.CancellationPending
    End Sub
    Private Sub bwSearch_RunWorkerCompleted(ByVal sender As System.Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles bwSearch.RunWorkerCompleted
        Me.pnlSearch.Visible = False
    End Sub

End Class